function getVar(variable) {

	var query = window.location.search.substring(1);
	var vars = query.split("&");
  	for (var i=0;i<vars.length;i++) {

    	var pair = vars[i].split("=");

    	if (pair.length == 2){

	    	if (pair[0] == variable) {

				if (pair[1].indexOf('_RM_REDIR_') > 0 && pair[1].indexOf('url%3D') == '-1'){
					pair[1]+="=";
					return pair[1];
				} else {
					return pair[1];
				}

			}
		} else {

			var var_position = query.indexOf(variable);

			if (var_position != -1){

				var var_lenght = variable.length + 1;

				var temp_str = query.slice(var_position+var_lenght,999999);

 				var var_position2 = temp_str.indexOf("&");

				if (var_position2 != -1){
					temp_str = temp_str.slice(0,var_position2);

				//	alert(temp_str);

 				}


				return temp_str;

			} else {
				break;
			}
		}
	}
}

function checkFlash_old(){

	var MM_contentVersion = 6;
	var plugin = (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]) ? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin : 0;

	if ( plugin ) {
			var words = navigator.plugins["Shockwave Flash"].description.split(" ");
			for (var i = 0; i < words.length; ++i)
			{
			if (isNaN(parseInt(words[i])))
			continue;
			var MM_PluginVersion = words[i];
			}
		var MM_FlashCanPlay = MM_PluginVersion >= MM_contentVersion;
	}
	else if (navigator.userAgent && navigator.userAgent.indexOf("MSIE")>=0
	   && (navigator.appVersion.indexOf("Win") != -1)) {
		document.write('<SCR' + 'IPT LANGUAGE=VBScript\> \n'); //FS hide this from IE4.5 Mac by splitting the tag
		document.write('on error resume next \n');
		document.write('MM_FlashCanPlay = ( IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash." & MM_contentVersion)))\n');
		document.write('</SCR' + 'IPT\> \n');
	}
	if ( MM_FlashCanPlay ) {
		return true;
	} else{
		return false;
	}
}

function ControlVersion()
{
	var version;
	var axo;
	var e;

	// NOTE : new ActiveXObject(strFoo) throws an exception if strFoo isn't in the registry

	try {
		// version will be set for 7.X or greater players
		axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
		version = axo.GetVariable("$version");
	} catch (e) {
	}

	if (!version)
	{
		try {
			// version will be set for 6.X players only
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");

			// installed player is some revision of 6.0
			// GetVariable("$version") crashes for versions 6.0.22 through 6.0.29,
			// so we have to be careful.

			// default to the first public version
			version = "WIN 6,0,21,0";

			// throws if AllowScripAccess does not exist (introduced in 6.0r47)
			axo.AllowScriptAccess = "always";

			// safe to call for 6.0r47 or greater
			version = axo.GetVariable("$version");

		} catch (e) {
		}
	}

	if (!version)
	{
		try {
			// version will be set for 4.X or 5.X player
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
			version = axo.GetVariable("$version");
		} catch (e) {
		}
	}

	if (!version)
	{
		try {
			// version will be set for 3.X player
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
			version = "WIN 3,0,18,0";
		} catch (e) {
		}
	}

	if (!version)
	{
		try {
			// version will be set for 2.X player
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
			version = "WIN 2,0,0,11";
		} catch (e) {
			version = -1;
		}
	}
//	alert(version);
	return version;
}

function checkFlash(){

	var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
	var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
	var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;


	// NS/Opera version >= 3 check for Flash plugin in plugin array
	var flashVer = -1;

	if (navigator.plugins != null && navigator.plugins.length > 0) {
		if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
			var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
			var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
			var descArray = flashDescription.split(" ");
			var tempArrayMajor = descArray[2].split(".");
			var versionMajor = tempArrayMajor[0];
			var versionMinor = tempArrayMajor[1];
			var versionRevision = descArray[3];
			if (versionRevision == "") {
				versionRevision = descArray[4];
			}
			if (versionRevision[0] == "d") {
				versionRevision = versionRevision.substring(1);
			} else if (versionRevision[0] == "r") {
				versionRevision = versionRevision.substring(1);
				if (versionRevision.indexOf("d") > 0) {
					versionRevision = versionRevision.substring(0, versionRevision.indexOf("d"));
				}
			}
			var flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
	//		alert("flashVer="+flashVer);
		}
	}
	// MSN/WebTV 2.6 supports Flash 4
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
	// WebTV 2.5 supports Flash 3
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
	// older WebTV supports Flash 2
	else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
	else if ( isIE && isWin && !isOpera ) {
		flashVer = ControlVersion();
	}


	if (flashVer != -1){
	    if (flashVer.indexOf("WIN") != -1){
			var tmp = flashVer.split("WIN");
			flashVer = tmp[1];

		}

		if (flashVer.indexOf(",") != -1){
	        flashVer = flashVer.replace(",",".");
		}

		var versionNumber = flashVer.split(".");

		if (versionNumber[0] < 10)
		    flashVer = -1;

	//	if (navigator.userAgent.toLowerCase().indexOf("safari") != -1)
	//	    flashVer = -1;

	}

	return flashVer;
}

function getCookie(c_name)
{
	if (document.cookie.length>0) {
  		c_start=document.cookie.indexOf(c_name + "=");

		if (c_start!=-1) {

    		c_start=c_start + c_name.length+1;
    		c_end=document.cookie.indexOf(";",c_start);
    		if (c_end==-1)
				c_end=document.cookie.length;

		return unescape(document.cookie.substring(c_start,c_end));
		}
	}

	return "";
}

function loadBanner(){

	var cookie = getCookie('bvs');
	var width = getVar('w');
	var height = getVar('h');
	var banner = getVar('b');
	var directory = getVar('d');
	var debug = getVar('debug');

	if (!banner)
		banner = "cpm_xmldefault_01_";

	if (!directory)
		directory = "xmldefault";

	if (!width)
		width = 728;

	if (!height)
		height = 90;

	if (debug == 'cookie')
	{
		alert(cookie);
	}


	var url_banner_flash = "http://img.catho.com.br/ads/banners/"+directory+"/"+banner+"_"+width+"x"+height+".swf";

	var url_banner_normal = "http://img.catho.com.br/ads/banners/"+directory+"/"+banner+"_"+width+"x"+height+".gif";

	var clicktag = getVar("c");


	if (!clicktag)
		clicktag = "http://www.catho.com.br";

	/*
	// FIX ADICIONADA PARA FUNCIONAR A CLICKTAG DO IG
    var temp_pos = clicktag.indexOf("%3D=")+3;
	if (temp_pos >0){
		clicktag = clicktag.substring(0,temp_pos);
	}
	*/

	var str_banner_flash = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="'+width+'" height="'+height+'">';
	str_banner_flash +='<PARAM NAME=movie VALUE="'+url_banner_flash+'?oas='+clicktag+'&'+cookie+'"><PARAM NAME=loop VALUE=true><PARAM NAME=menu VALUE=false><param NAME=wmode Value=opaque><PARAM NAME=quality VALUE=high>';
	str_banner_flash +='<EMBED src="'+url_banner_flash+'?oas='+clicktag+'&'+cookie+'" loop=true menu=false quality=high bgcolor=#FFFFFF wmode=transparent swLiveConnect=FALSE WIDTH="'+width+'" HEIGHT="'+height+'" TYPE="application/x-shockwave-flash">';
	str_banner_flash +='</OBJECT>';

	if (debug == 'swf')
	{
		alert(str_banner_flash);
	}

	/* complemento da clicktag para o banner em gif caso ela seja vazia
	var temp = clicktag.lastIndexOf("url%3D");

	if (clicktag.slice(temp+6,9999) == ""){

		clicktag += "http://www.catho.com.br";
	}

 	*/

	var str_banner_normal = '<a href="'+unescape(clicktag)+'" target="_blank"><img src="'+url_banner_normal+'" width="'+width+'" height="'+height+'" border=0></a>';

	if (debug == 'img')
	{
		alert(str_banner_normal);
	}


	var flash = checkFlash();

	if (flash != -1){
		document.write(str_banner_flash);
	} else {
		document.write(str_banner_normal);
	}


}

function loadCampaign(clicktag,banner,directory,width,height){

	var path = "http://img.catho.com.br/ads/loadbanner.html?w="+width+"&h="+height+"&b="+banner+"&d="+directory+"&c="+escape(clicktag);

	var str_iframe = '<iframe src="'+path+'" frameborder=0 marginwidth=0 framespacing=0 marginheight=0 scrolling=no width="'+width+'" height="'+height+'"></iframe>';
	return str_iframe;

}